#!/usr/bin/python

from __future__ import division, print_function
from rsl import RSL
import math
import readline
import sys

class Executor():
	def execute(self):
		interpreter = RSL();
		interpreter.engine.before_error = "X\n"
		
		try:
			filename = sys.argv[1]
		except:
			filename = raw_input("Please enter an RSB filename: ")
		
		try:
			sys.stdout.write("Reading file... ")
			f = open(filename)
			bytecode = f.read()
			f.close()
			sys.stdout.write("done.\n")
		except IOError:
			print("Invalid filename entered.")
			return
		
		# Get a new filename before running this
		n = filename.rfind(".")
		if (n == -1):
			xfilename = filename + ".rsl"
			ofilename = filename + ".rso"
		else:
			xfilename = filename[:n] + ".rsl"
			ofilename = filename[:n] + ".rso"
		print("Main bytecode has been read from: " + filename)
		print("Class and function bytecode has been read from: " + ofilename)
		
		print("Executing main bytecode: ")
		print("============================================")
		
		interpreter.loadByteCodeObject(ofilename)
		interpreter.runWithoutWriteByteCode(bytecode, xfilename)
	
print("Welcome to the RSL Executor.")
com = Executor()
com.execute()